﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using FANUC.DTO;

namespace FANUC.Controllers
{
    public class TodayRobotOutputsController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        [ResponseType(typeof(TodayRobotOutputsDtoOutput))]
        public IHttpActionResult Get(int id)
        {
            #region Test
            if (Helper.IsTest)
            {
                TodayAllRobotsOutputsDtoOutput testData = new TodayAllRobotsOutputsDtoOutput();
                testData.Date = System.DateTime.Now.Date;
                testData.OutputsQuantity = 10000;
                return Ok(testData);
            }
            #endregion
            DateTime today = System.DateTime.Now.Date;
            var robot = db.Robot.Where(a => a.ID == id).FirstOrDefault();

            int count = 0;
            switch (robot.Robot_Type.Type)
            {
                case "Arc Welding":
                    count = db.Part_Unit.Where(a => a.Robot_ID == id && a.Start_Time >= today).Count();
                    break;
                case "Spot Welding":
                    count = db.Spot_Part_Unit.Where(a => a.Robot_ID == id && a.Start_Time >= today).Count();
                    break;
                case "Seal":
                    count = db.Seal_Part_Unit.Where(a => a.Robot_ID == id && a.Start_Time >= today).Count();
                    break;
                case "Press":
                    count = db.Press_Part_Unit.Where(a => a.Robot_ID == id && a.Start_Time >= today).Count();
                    break;
                case "Polish":
                    count = db.Polish_Part_Unit.Where(a => a.Robot_ID == id && a.Start_Time >= today).Count();
                    break;
            }
            TodayRobotOutputsDtoOutput data = new TodayRobotOutputsDtoOutput();
            data.Date = today;
            data.OutputsQuantity = count;
            data.RobotID = id.ToString();
            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        { return null; }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
